/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "OpMULNode.h"
#include "OpADDNode.h"
#include "ValueNode.h"
static Real opMUL(const Real& left, const Real& right) {
    return left * right;
}

static SmartPtr<ExpressionTreeNode> opMULDerivativeBuilder(SmartPtr<ExpressionTreeNode> node, const QString& varName) {
    if(!node)
        return 0;
    if(node->info()!=MUL_SYMBOL)
        return 0;
    if(!node->right())
        return 0;
    if(!node->left())
        return 0;

    SmartPtr<ExpressionTreeNode> root = 0;
    SmartPtr<ExpressionTreeNode> left = 0;
    SmartPtr<ExpressionTreeNode> right = 0;
    SmartPtr<ExpressionTreeNode> dleft = 0;
    SmartPtr<ExpressionTreeNode> dright = 0;

    dleft = node->left()->derive(varName);
    if(!dleft)
        return 0;
    dright = node->right()->derive(varName);
    if(!dright){
        return 0;
    }
    left = node->left()->copy();
    right = node->right()->copy();

    root = new OpMULNode();
    root->setLeft(dleft);
    root->setRight(right);
    dleft = root;

    root = new OpMULNode();
    root->setLeft(left);
    root->setRight(dright);
    dright = root;

    root = new OpADDNode();
    root->setLeft(dleft);
    root->setRight(dright);

    SmartPtr<ExpressionTreeNode> tmp = root->simplified();
    return tmp;
}

OpMULNode::OpMULNode()
        :ArithmeticOpNode(MUL_SYMBOL, &opMUL, &opMULDerivativeBuilder){
}

ExpressionTreeNode* OpMULNode::copy() const {
    SmartPtr<ExpressionTreeNode> left = 0;
    SmartPtr<ExpressionTreeNode> right = 0;
    ExpressionTreeNode *root = 0;
    if(_left)
        left = _left->copy();
    if(_right)
        right = _right->copy();
    root = new OpMULNode();
    root->setLeft(left);
    root->setRight(right);
    return root;
}

int OpMULNode::precedence() const {
    return MUL_PRECEDENCE;
}

SmartPtr<ExpressionTreeNode> OpMULNode::firstLevelSimplification() const {
    SmartPtr<ExpressionTreeNode> root = 0;
    QString branchPath;
    if(!_left || !_right)
        return 0;
    SmartPtr<ExpressionTreeNode> left = _left->firstLevelSimplification();
    if(!left)
        return 0;
    SmartPtr<ExpressionTreeNode> right = _right->firstLevelSimplification();
    if(!right){
        return 0;
    }
    if(left->isValue() && right->isValue() && !left->isMathConstant() && !right->isMathConstant()){
        root = new ValueNode((*_op)(left->eval(),right->eval()));
        return root;
    }

    if(left->isValue()) {
        if(left->eval().isZero()) {
            return new ValueNode(0.0);
        }
        if(left->eval()==1.0) {
            return right;
        }
    }
    if(right->isValue()) {
        if(right->eval().isZero()) {
            return new ValueNode(0.0);
        }
        if(right->eval()==1.0) {
            return left;
        }
    }

    root = new OpMULNode();
    root->setLeft(left);
    root->setRight(right);
    return root;
}

QString OpMULNode::toInfixString() const {
    QString branchPath;
    QString leftString = _left->toInfixString();
    QString rightString = _right->toInfixString();
    if((_right->isBinaryOp() && (_right->precedence()<precedence())) ||
       (_right->isUnaryOp() && (_right->info()=="-"))||
       (_right->isValue() && (_right->eval()<0))){
        rightString = "("+rightString+")";
    }
    if(_left->isOp() && (_left->precedence()<precedence())){
        leftString = "("+leftString+")";
    }
    return leftString+MUL_SYMBOL+rightString;
}

